import java.io.*;
import java.util.*;
/**
Generic application layer packet for ASSP.
<command> <uid> CRLF
<body> CRLF
*/
public class Packet{
	public String command;
	public String uid;
	public String body;
	/** Most preferred constructor I think. 
	Notes:
		> find out what happens when readLine tries to process empty line terminated by CRLF
		> Figure out how to represent null values. for instance: 'logout' does not require any arguments
		> nextToken throws NoSuchElementException
		> readLine assumed to throw IOException if inputstream is disconnected
	*/
	public Packet(BufferedReader cin)throws Exception{
			command="";
			uid="";
			String line=cin.readLine();
			line=line.trim();
			String w[]=line.split(" ");
			for(int i=0;i<(w.length<2?w.length:2);i++){
				if(i==0)
					command=w[i];
				else if(i==1)
					uid=w[i];
			}
			body=cin.readLine();
			if(body==null)
				body="";
	}/*</Packet>*/
	/*******************************************************************
	 * @param command
	 * @param uid
	 * @param body
	 * @return the packaged abstraction
	 ******************************************************************/
	public Packet(String command, String uid, String body){
		this.command=command;
		this.uid=uid;
		this.body=body;
	}/*</Packet>*/
	/*******************************************************************
	 * Representation to be pushed into the TCP layer.
	 * @return the tokens delimted according to RFC
	 ******************************************************************/
	public String toString(){
		return 
			command + " " + uid + " " + ASSServer.CRLF +
			body    + ASSServer.CRLF;
	}/*</toString>*/
	public String toDelimitedString(){
		return "<"+command+">"+"<"+uid+">"+"<"+body+">";
	}/*</toDelimitedString>*/
}/*</Packet>*/
